 <?php


function correct_unit($num, $sing, $plu)
{	
	if($num == 1) return $sing; 
	//else if($num == 0) echo ""; 
	else return $plu;
}


function fetch_gp_details($gpid, $type = "")
{
		$res = mysql_query("SELECT * FROM site_groups WHERE `id`='$gpid'");
		if(mysql_numrows($res) == 0) return;
		$gpname = mysql_result($res,0,'name');
		$fbid = mysql_result($res,0,'fbid');
		$time = mysql_result($res,0,'timestamp');
		$visibility = mysql_result($res,0,'visibility');
		
		$count_posts = mysql_numrows(mysql_query("SELECT * FROM site_posts WHERE `type`='group_$gpid' AND `del`!='1'")); // number of members				 
		$count = mysql_numrows(mysql_query("SELECT * FROM site_group_members WHERE 	`gpid`='$gpid' AND `del`!='1'"));  // number of members 
		
		// to get the name and 
		$arrtemp = explode("_", $type);
		$type = $arrtemp[0];
		$loggedin_user = $arrtemp[1];
		
		// setting the group name in case no name was specified by the creator
		if($gpname == "")
		{
				$gpname = "";	
				$mem_summary = mysql_query("SELECT * FROM site_group_members WHERE `gpid`='$gpid' AND `del`!='1' LIMIT 2"); 
				// in case there is only one member. then the number of times, the loop runs needs to be one
				if($count < 2) $n = $count;
				else $n = 2;
				
				// running the loop to gather user info
				for($i=0;$i<$n;$i++)
				{
					$temp = mysql_result($mem_summary,$i,'username');
					$temp1 = mysql_result($mem_summary,$i,'fbid');			
				
					
					if($arrtemp[1] == $temp1)
						$gpname1 = "you";
					else if($temp == "")
					{
						// extracting the name from the site_user_meta table
						$gpname1 = mysql_result(mysql_query("SELECT `name` FROM site_user_meta WHERE `fbid`='$temp1'"), 0, 'name');
					}
					else
						$gpname1 = $temp;
					
					if($i == $n-1)
						$gpname2 = "";
					else
						$gpname2 = ", ";
						
					$gpname .= $gpname1.$gpname2;
				}
				
				if($count <= 2)
				$moremem = "";
				else if(($count > 2))
				$moremem = " and ".($count-2)." more";
				
				$gpname = '<span><strong>[</strong>'.$gpname.$moremem.'<strong>]</strong></span>';						
		}
		
		
		if($type == "link")
		{
			echo '<div style="border-bottom:1px dashed #aaa;margin-bottom:10px;margin-top:10px;padding:5px;">';
			echo '<div id="group_'.$gpid.'_sidebarlink" class="a groupLink" style="width:220px;float:left">'.$gpname.'</div> ';
			echo '<div style="float:right" id="groupLeave_'.$gpid.'_sidebarlink" class="small-font groupLeaveLink" style="width:20px;" onclick=\'leave_circle("'.$gpid.'","","","short")\'>	<img src="images/cross.png" ></div>';		
			// if there is unread posts available
			// latest time stamp 
			
			$user_temp = mysql_query("SELECT * FROM site_read WHERE `type`='group_$gpid' AND `fbid`='$loggedin_user' ORDER BY `id` DESC LIMIT 1");
			$gptime_temp = mysql_query("SELECT * FROM site_posts WHERE `type`='group_$gpid' AND `del`!='1' ORDER BY `id` DESC LIMIT 1");
			if(mysql_num_rows($user_temp) != 0 && mysql_num_rows($gptime_temp) != 0) {
			$user_gp_time = mysql_result($user_temp, 0, 'timestamp');
			$gp_time = mysql_result($gptime_temp, 0, 'timestamp');
			if($gp_time > $user_gp_time)
				echo '<div style="float:right;margin:0px;margin-right:5px" class="circle unread"></div>';		
			}	
			echo '<div class="clear"></div>';
			echo '<div class="faded group_friendList_handle" style="text-decoration:none;padding:3px" onclick=\'$("#group_'.$gpid.'_friendsList").slideToggle();\'> 
			<img src="images/downarrow.png" style="vertical-align:middle"/> &nbsp;'.$count.' members and '.$count_posts.' posts</div>';
			
			echo '<div id="group_'.$gpid.'_friendsList" class="group_friendList" style="display:none"></div>';
			echo '</div>';
		}
		else if($type == "short" || $type == "")
		{		
			
			echo '<strong>'.$gpname.'</strong>';					
			echo '<br>  '.$count.' '.correct_unit($count, "member", "members").'  '.$count_posts.' '.correct_unit($count_posts,"post","posts"); 
			
			if($type == "") {
				$user_name_dump = mysql_query("SELECT * FROM site_user_meta WHERE `fbid`='$fbid'");
				$user_name = mysql_result($user_name_dump, 0, 'name');
						
				$time_n = time();
				$diff = $time_n - $time;
			
				if($diff < 60) 		{ $val = $diff; if($val == '1') $unit = "second"; else $unit = "seconds";}
				elseif($diff < 3600)		{ $val = floor($diff/60); if($val == '1') $unit = "minute"; else $unit = "minutes";}
				elseif($diff < (24*3600)) 		{ $val = floor($diff/60/60); if($val == '1') $unit = "hour"; else $unit = "hours";}
				elseif($diff < (100*24*3600))		{ $val = floor($diff/60/60/24); if($val == '1') $unit = "day"; else $unit = "days";}
				else { $val = ""; $unit = "long time";}		
				echo '<br>Created '.$val.' '.$unit.' ago by <a href="http://facebook.com/'.$fbid.'" target="_blank">'.$user_name.'</a>';		
			}
			
			echo '</span>';
		}				
}

function disp_user($result, $type, $user, $format="general") 
{
			
		// would expect the result to have a variable $fbid 	
		$arr = explode("_",$type);
		$type1 = $arr[0];
		
		// if type is that for a group or for add requests in a group, set the $gpid 
		if($type1 == "group" || $type1 == "requests") 
		$gpid = $arr[1];
		
		if($format == "general")
		{
			echo '<table>';
			$count = 0;
		}
		if($format == "group")
		{
			echo '<div>';
			echo '<input type="text" placeholder="add friends" class="addFriendsField" id="'.$type.'_addFriendsField" style="width:200px" onkeyup=\'filter_frndlst2(this, event)\'/>';
			echo '<div id="'.$type.'_friendsListSuggestions" style="width:200px;position:relative"></div>';
			echo '</div>';		
			echo '<div id="'.$type.'_membersList">';
		}
		
		
		for($i=0;$i<mysql_numrows($result);$i++)
		{
			if($format == "general")
			{
			if($count == 2) $count = 1;
			else  $count++; 
			
			if($count == 1) echo '<tr><td valign="top">';
			else echo '<td valign="top">';
			}
			
			$fbid = mysql_result($result, $i, 'fbid');
			
			// setting the name
			// in case its for a group for members who haven't joined yet 
			$temp = mysql_query("SELECT * FROM site_user_meta WHERE `fbid`='$fbid'");
			if(mysql_numrows($temp) == 0)
			{
				$temp1 = mysql_query("SELECT * FROM site_group_members WHERE `fbid`='$fbid'");
				$username = mysql_result($temp1, 0, 'username');
			} else {
				$username = mysql_result($temp, 0, 'name');	
			}
			
			
			
			if($format == "reduced")
			{
				//echo '<div style="float:left;padding:5px">'.is_user_online($fbid,"reduced").'</div>';
				echo '<div style="float:left;padding:5px">
				<a href="http://facebook.com/'.$fbid.'" target="_blank">'.$username.'</a>
				</div>';				
				echo '<div class="clear"></div>';
			}
			
			// this part is for displaying in the header menu whose id has to be different from the "reduced" one
			if($format == "online")
			{
				echo '<div style="float:left;padding:5px" id="user_'.$fbid.'_circle_menu" class="online_stat">'.is_user_online($fbid,"reduced").'</div>';
				echo '<div style="float:left;padding:5px"><a href="http://facebook.com/'.$fbid.'" target="_blank">'.$username.'</a></div>';				
				echo '<div class="clear"></div>';
			}
			
			if($format == "group")
			{
				echo '<div>';
				echo '<div style="float:left;padding:5px" id="user_'.$fbid.'_circle_menu" class="online_stat">'.is_user_online($fbid,"reduced").'</div>';
				echo '<div style="float:left;padding:3px">
				<a href="http://facebook.com/'.$fbid.'" target="_blank">'.$username.'</a>';
				if(mysql_num_rows(mysql_query("SELECT * FROM site_user_meta WHERE `fbid`='$fbid'")) == 0)
				echo '<span id="user_'.$fbid.'_invite_'.$gpid.'" class="online_stat">'.is_user_online($fbid,"invite_".$gpid."").'</span>';
				echo '</div>';								
				echo '<div class="clear"></div>';
				echo '</div>';				
			}
			
			if($format == "general" || $format == "return")
			{
				$output = "";
				$output = $output.'<table class="shadow" style="margin:10px;width:250px">
				<tr>
				<td style="width:70px">';
				$output = $output.'<a href="http://facebook.com/'.$fbid.'" target="_blank"><img src="http://graph.facebook.com/'.$fbid.'/picture" /></a>';
				$output = $output.'</td>';
				
				if($type1 == "group") // checking the admin | mod for further use
				{
				// checking if the displayed user is the admin of the group
				$admin_yon = mysql_numrows(mysql_query("SELECT * FROM site_groups WHERE `id`='$gpid' AND `fbid`='$fbid' AND `del`!='1'"));
				$admin_present_yon = mysql_numrows(mysql_query("SELECT * FROM site_groups WHERE `id`='$gpid' AND `fbid`='$user' AND `del`!='1'"));
				
				// checking if the displayed user is the mod
				$mod_yon = mysql_numrows(mysql_query("SELECT * FROM site_group_members WHERE `fbid`='$fbid' AND `gpid`='$gpid' AND `mod`='1' AND `del`!='1'"));
				}
				
				//displaying name 
				$output = $output.'<td valign="top">
				<a href="http://facebook.com/'.$fbid.'" target="_blank">'.$username.'</a>
				<br>';
			
			// online or offline
				$output = $output.'<span id="user_'.$fbid.'_'.$type.'" class="online_stat">';
				$output = $output.is_user_online($fbid);
				$output = $output.'</span>'; 
							
				//  add request link to add a member to group			
				if($type1 == "requests")
				{
					$output = $output.' | <span class="small-font a" onclick="allow_gp_request(this,'.$gpid.','.$fbid.')">add user</span>';
				} 
				$output = $output.'</td>'; 
				
				//displaying whether the user is admin or mod
				if($type1 == "group")
				{
				if($admin_yon != 0)	$output = $output.'<td style="width:20px;background:#afa"><strong ></strong></td>';
				else if($mod_yon != 0)	$output = $output.'<td style="width:20px;background:#faa"><strong></strong></td>';
				else $output = $output.'<td style="width:20px;background:#fff"><strong></strong></td>';
				}
				else $output = $output.'<td style="width:20px;background:#fff"><strong></strong></td>';
				
				// options to make/remove mod and remove user and viewable to the admin
				if($type1 == "group" && $admin_present_yon!=0)
				{
					if($admin_yon == 0) // obviously these options don't apply to the admin
					{
					if($mod_yon == 0) 
					$output = $output.'</tr><tr style="background:#fcc"><td colspan="3"><span class="small-font a" onclick=\'make_gp_mod('.$gpid.','.$fbid.',"make", this)\'>Make mod</span>';
					else 
					$output = $output.'</tr><tr style="background:#fcc"><td colspan="3"><span class="small-font a" onclick=\'make_gp_mod('.$gpid.','.$fbid.',"rem", this)\'>Remove mod</span>';
					// for removing the user	
					$output = $output.'&nbsp; <span class="small-font a" onclick="del_gp_user('.$gpid.','.$fbid.', this)">Remove user</span></td>';
					}
				}
		
				$output = $output.'</tr></table>';
					$output = $output.'</td>';
					if($count == 2) $output = $output.'</tr>';
				
				// if the values are needed as part of the variable $output
				if($format == "return")
					return $output;
			}
		}
		
		if($format == "general")
		{
			if($count == 1) echo '<td></td></tr>';
			echo '</table>';
		}
		if($format == "group")
		{
			echo '</div>';
		}
		
}

function fetch_user_details($fbid,$type="gen")
{
	//$db = new database('localhost', 'roopunkc_gausipp', 'thedynamicme', 'roopunkc_gausipp');
	$result = mysql_query("SELECT * FROM site_user_meta WHERE `fbid`='$fbid'");
	$name = mysql_result($result,0,'name');

	// ccalculating number of groups that the user has
	$res1 = mysql_query("SELECT * FROM site_group_members WHERE `fbid`='$fbid' AND `del`!='1'");
	$num_groups = mysql_numrows($res1);
	
	echo '	
	<table style="width:100%">
	<tr>
	<td style="width:70px">
	';
	if($type == "chat") {
	echo '<div><img src="images/icons/chat1.png" style="width:50px"/></div>';
	}
	else {
	echo '<a href="http://facebook.com/'.$fbid.'" target="_blank"><img src="http://graph.facebook.com/'.$fbid.'/picture" /></a>';		
	}
	
	echo '</td>
	<td>
	<span style="font-size:15pt" class="cfont1">'.$name.'</span>
	<span id="user_'.$fbid.'_gen" class="online_stat">'.is_user_online($fbid).'
	</span><br>
	'.$num_groups.' '.correct_unit($num_groups, 'circle','circles').'
	<br>';
	echo '</td>
	
	</tr>
	</table>
	';
}


function subscribe_status($fbid, $gpid)
{
	//$db = new database('localhost', 'roopunkc_gausipp', 'thedynamicme', 'roopunkc_gausipp');

	$result = mysql_query("SELECT * FROM site_subscribe WHERE `fbid`='$fbid' AND `gpid`='$gpid' AND `del`!='1'");
	if(mysql_numrows($result) == 0)
		return '<span class="a" onclick=\'subscribe_group('.$fbid.', '.$gpid.', "subscribe", this)\'>Subscribe</span>';
	else
		return '<span class="a" onclick=\'subscribe_group('.$fbid.', '.$gpid.', "unsubscribe", this)\'>Unsubscribe</span>';
}



?>